package org.itsky.study.test4;

/**
 * @Author sky
 * @Site cmtianxie163.com 2020/4/16 10:56
 */
class TicketThread extends Thread{
    /**
     * 因为每个线程都是创建了一个TicketThread类的实例对象，所以每个对象都有100张票 ，但是票总数应该是所有线程共享的数据
     * 所以归类所有 用static修饰
     */
//    private int ticket = 100;

    /**
     * 还是存在线程安全问题
     *
      */
    private static int ticket = 100;
    @Override
    public void run() {
        while (true){
            if (ticket>0){
                System.out.println(Thread.currentThread().getName() + "正在售票，票号为" +(101-ticket));
                ticket --;
            }else{
                break;
            }
        }
    }
}

public class TicketTest {
    public static void main(String[] args) {
        TicketThread t1 = new TicketThread();
        t1.setName("窗口1");
        TicketThread t2 = new TicketThread();
        t2.setName("窗口2");
        TicketThread t3 = new TicketThread();
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }


}
